//
//  Team9ExpertIdeabookCell.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/16.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9ExpertIdeabookCell.h"
#import "Team9JQHeader.h"
#import "Team9ExpertIdeabookModel.h"

@interface Team9ExpertIdeabookCell ()

@property(nonatomic, strong)NSMutableArray<UIImageView *> *imageViews;
@property(nonatomic, strong)UILabel *titileLabel;
@property(nonatomic, strong)UILabel *itemCountLabel;

@end
@implementation Team9ExpertIdeabookCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
//        [self test];
    }
    return self;
}
- (void)test{
    for (UIImageView *imageView in _imageViews) {
        imageView.image = [UIImage imageNamed:@"like"];
    }
}
- (void)createView{
    
    
    UIImageView *imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"111"];
    [self.contentView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    self.imageViews = @[].mutableCopy;
    for (int i = 0; i < 4; i++) {
        UIImageView *imageView = [UIImageView new];
        [self.contentView addSubview:imageView];
        [self.imageViews addObject:imageView];
    }
    
    self.titileLabel = [UILabel new];
    [self.contentView addSubview:self.titileLabel];
    
    self.itemCountLabel = [UILabel new];
    [self.contentView addSubview:self.itemCountLabel];
    
    self.titileLabel.textColor = [UIColor whiteColor];
    self.titileLabel.alpha = 0.8;
    self.titileLabel.backgroundColor = [UIColor blackColor];
    self.titileLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    
    self.itemCountLabel.textColor = [UIColor whiteColor];
    self.itemCountLabel.alpha = 0.8;
    self.itemCountLabel.backgroundColor = [UIColor blackColor];
    self.itemCountLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.itemCountLabel.textAlignment = NSTextAlignmentCenter;
    
    [self p_contraints];
}
- (void)p_contraints{
    UIImageView *imageView1 = self.imageViews[0];
    UIImageView *imageView2 = self.imageViews[1];
    UIImageView *imageView3 = self.imageViews[2];
    UIImageView *imageView4 = self.imageViews[3];
    
    [imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self);
        make.height.equalTo(imageView1.mas_width);
    }];
    [imageView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView1);
        make.left.equalTo(imageView1.mas_right).offset(1);
        make.height.equalTo(imageView1.mas_width);
        make.width.equalTo(imageView1);
    }];
    [imageView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self);
        make.top.equalTo(imageView1.mas_bottom).offset(1);
        make.left.equalTo(imageView1);
        make.width.equalTo(imageView1);
        make.height.equalTo(imageView1.mas_width);
    }];
    [imageView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imageView3);
        make.left.equalTo(imageView3.mas_right).offset(1);
        make.height.equalTo(imageView1.mas_width);
        make.width.equalTo(imageView1);
    }];
    
    [self.titileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView3).offset(5);
        make.right.lessThanOrEqualTo(imageView2).offset(-5);
        make.bottom.equalTo(imageView3).offset(-5);
    }];
    [self.itemCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(imageView2).offset(-5);
        make.top.equalTo(imageView2).offset(5);
        make.width.greaterThanOrEqualTo(self.itemCountLabel.mas_height).multipliedBy(1.2);
    }];
}
- (void)setModel:(Team9ExpertIdeabookModel *)model
{
    if (_model != model) {
        _model = model;
    }
    for (int i = 0; i < 4; i++) {
        if (i < self.model.items.count) {
#warning 没有占位图
            [_imageViews[i] sd_setImageWithURL:[NSURL URLWithString:self.model.items[i][@"pic"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                [UIView animateWithDuration:0.3 animations:^{
                    CATransition *transition = [CATransition animation];
                    transition.type = @"fade";
                    [transition setDuration:0.5];
                    [transition setRepeatCount:1];
                    // 向imageView上添加动画效果
                    [_imageViews[i].layer addAnimation:transition forKey:@"transition"];
                }];
            }];
        }
    }
    self.titileLabel.text = self.model.title;
    self.itemCountLabel.text = [NSString stringWithFormat:@"%ld", self.model.items.count];
}
@end
